package com.qjcc.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 采用SharedPreferences操作用户信息
 * Created by WangKun on 2015/09/15.
 */
public class SharedPreferencesUtil {
    /**
     * 获取SharedPreferences对象
     * @param activity
     * @return
     */
    public static SharedPreferences getSharedPreferences(Activity activity){
        return activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
    }

    /**
     * 保存登录用户信息到app中
     * @param activity 当前activity
     * @param userMap 用户信息
     */
    public static void saveUser(Activity activity, Map userMap){
        SharedPreferences sharedPreferences = activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
        if(null != userMap){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            for (Iterator<Map.Entry<String, String>> it = userMap.entrySet().iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                editor.putString(entry.getKey(), entry.getValue());
            }
            //我将用户信息保存到其中，也可以保存登录状态
            editor.commit();
        }
    }

    /**
     * 获取当前保存的用户所有信息
     * @param activity
     * @return
     */
    public static Map<String, String> getLoginUserMap(Activity activity){
        Map<String, String> userMap = new HashMap();
        SharedPreferences sharedPreferences = activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
        userMap.put("userId", sharedPreferences.getString("userId", null));
        userMap.put("userName", sharedPreferences.getString("userName", null));
        userMap.put("password", sharedPreferences.getString("password", null));
        userMap.put("tokenCode", sharedPreferences.getString("tokenCode", null));
        userMap.put("isReLogin", sharedPreferences.getString("isReLogin", null));
        return userMap;
    }

    /**
     * 获取当前登录用户id
     * @param activity
     * @return
     */
    public static String getLoginUserId(Activity activity){
        SharedPreferences sharedPreferences = activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
        return sharedPreferences.getString("userId", null);
    }

    /**
     * 获取当前登录用户id
     * @param context
     * @return
     */
    public static String getLoginUserId(Context context){
        SharedPreferences sharedPreferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        if (sharedPreferences != null) {
            return sharedPreferences.getString("userId", null);
        }
        return null;

    }

    /**
     * 清除用户保存用户信息
     * @param activity
     */
    public static void removeLoginUserInfo(Activity activity){
        SharedPreferences sharedPreferences = activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        // 清空
        editor.clear();
        editor.commit();
    }

    /**
     * 验证是否登录成功
     * @param activity
     * @return
     */
    public static boolean isLogin(Activity activity){
        SharedPreferences sharedPreferences = activity.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
        if(null != sharedPreferences && null != sharedPreferences.getString("userId", null) && !"".equals(sharedPreferences.getString("userId", null))){
            return true;
        }
        return false;
    }
    /**
     * 获取缓存中的sessionId
     * @param context
     * @return
     */
    public static String getSessionId(Context context){
    	SharedPreferences sharePreferences = context.getSharedPreferences("userSessionId", Context.MODE_PRIVATE);
		return sharePreferences.getString("userSessionId", null);
    }
    /**
     * 保存sessionId
     * @param context
     * @param userSessionId
     */
    public static void saveSessionId(Context context,String userSessionId){
    	SharedPreferences sharedPreferences = context.getSharedPreferences("userSessionId", Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
    	if(userSessionId != null){
    		editor.putString("userSessionId", userSessionId);
    	}
    	editor.commit();
    }
    /**
     * 清除sessionId
     * @param context
     */
    public static void removeSessionId(Context context){
    	SharedPreferences sharedPreferences = context.getSharedPreferences("userSessionId", Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.clear();
		editor.commit();
    }
    /**
     * 保存引导状态
     * @param context
     */
    public static void saveGuideStatus(Context context){
    	SharedPreferences sharedPreferences = context.getSharedPreferences("isGuided", Context.MODE_PRIVATE);
    	SharedPreferences.Editor editor = sharedPreferences.edit();
    	editor.putBoolean("isGuided", true);
    	editor.commit();
    }
    
    public static boolean isGuided(Context context){
    	SharedPreferences sharePreferences = context.getSharedPreferences("isGuided", Context.MODE_PRIVATE);
		return sharePreferences.getBoolean("isGuided", false);
    }

    public static String getMac(Context context){
        SharedPreferences sharePreferences = context.getSharedPreferences("userMac", Context.MODE_PRIVATE);
        return sharePreferences.getString("userMac", null);
    }

    public static void saveMac(Context context,String mac){
        SharedPreferences sharedPreferences = context.getSharedPreferences("userMac", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if(mac != null){
            editor.putString("userMac", mac);
        }
        editor.commit();
    }
}
